package main

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
)
const DbConfigPath = "config/DbConfig.yaml"

type DbConfig struct {
	Host string `yaml:"host"`
	User string `yaml:"user"`
	Pwd string `yaml:"pwd"`
	Dbname string `yaml:"dbname"`
	Charset string `yaml:"charset"`
	MaxOpenConns int `yaml:"max-open-conns"`
	MaxIdleConns int `yaml:"max-idle-conns"`
	ConnMaxLifetime int `yaml:"conn-max-lifetime"`
}

func (dbConfig *DbConfig) getDbConf() *DbConfig {
	yamlFile, err := ioutil.ReadFile(DbConfigPath)
	if err != nil {
		fmt.Println(err.Error())
	}
	err = yaml.Unmarshal(yamlFile, dbConfig)
	if err != nil {
		fmt.Println(err.Error())
	}
	if dbConfig.MaxIdleConns < 2{
		dbConfig.MaxIdleConns=2
	}
	if dbConfig.MaxOpenConns < 5 {
		dbConfig.MaxOpenConns=5
	}
	return dbConfig
}